<?php
namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Api\BaseController;
use App\Http\Requests\ItemRequest;
use App\Http\Resources\Item;
use App\Http\Resources\ItemCollection;
use App\Item as ItemModel;
use App\Repositories\ItemRepository;
use App\Scopes\StatusScope;
use App\Traits\BaseApiFunc;
use Auth;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class ItemController extends BaseController {

	use BaseApiFunc;

	protected $model, $page_size;

	public function __construct(ItemRepository $item) {
		parent::__construct();

		$this->model = $item;
		$this->page_size = config('blog.admin_page.item_page');
	}

	public function topics(Request $request) {
		$page_size = (int) $request->query('page_size', 5);
		$page = (int) $request->query('page', 1);
		$key = $request->query('key', '');
		$cate_id = (int) $request->query('cate_id', '');
		$tag_id = (int) $request->query('tag_id', '');

		$query['title'] = $key ?: '';
		$query['cate_id'] = $cate_id ?: '';
		$query['tag_id'] = $tag_id ?: '';

		$total_nums = $this->model->countIndexRaws($query);
		$list = (new ItemCollection($this->model->indexPage($page, $page_size, $query)->load('cate', 'tags')))->type('index')->additional(['meta' => compact('total_nums', 'page')]);

		return $this->response->withCollectionSuccess($list);
	}

	public function recommends() {
		$ids = $this->model->limit(30)->pluck('id');
		if (!$ids) {
			return $this->response->withSuccess([]);
		}
		$ids = $ids->toArray();
		$list = Cache::remember('recommends', config('blog.cache.time'), function () use ($ids) {
			shuffle($ids);
			return $this->model->select('id', 'title')->whereIn('id', $ids)->whereHas('cate', function ($query) {
				return $query->where('status', 1);
			})->limit(6)->get();
		});
		return $this->response->withSuccess($list);
	}

	public function hot() {
		$list = Cache::remember('hot-topics', config('blog.cache.time'), function () {
			return $this->model->select('id', 'title', 'see_nums')->whereHas('cate', function ($query) {
				return $query->where('status', 1);
			})->orderBy('see_nums', 'desc')->limit(8)->get();
		});
		return $this->response->withSuccess($list);
	}

	public function topic($id) {
		$data = (new Item($this->model->getTopic($id)->load('comments', 'cate', 'tags', 'approves')))->type('topic');
		//设置访问量+1
		$this->model->getById($id)->increment('see_nums');
		return $this->response->withSuccess($data);
	}

	public function store(ItemRequest $request) {
		$this->model->store(array_merge(['user_id' => Auth::user()->id], $request->all()));
		$this->model->syncTags($request->get('tags'));
		return $this->response->withNoContent();
	}

	public function update(Request $request, $id) {
		$this->model = $this->model->withOutStatus();
		$this->model->update($id, $request->all());
		if ($request->get('tags')) {
			$this->model->syncTags($request->get('tags'));
		}
		return $this->response->withNoContent();
	}

	protected function getResourceCollection(Collection $collection) {
		return new ItemCollection($collection->load(['comments' => function ($query) {
			return $query->withoutGlobalScope(StatusScope::class);
		}])->load(['cate' => function ($query) {
			return $query->withoutGlobalScope(StatusScope::class);
		}])->load(['approves' => function ($query) {
			return $query->withoutGlobalScope(StatusScope::class);
		}]));
	}

	protected function getResource(ItemModel $collection) {
		return new Item($collection->load(['tags' => function ($query) {
			return $query->withoutGlobalScope(StatusScope::class);
		}]));
	}
}
